home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MotifIconFactory.java < prev    next >
Text File  |  1998-06-30  |  12KB  |  424 lines

  1. /*
  2.  * @(#)MotifIconFactory.java    1.10 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.motif;
  22.  
  23. import com.sun.java.swing.*;
  24.  
  25. import com.sun.java.swing.plaf.UIResource;
  26.  
  27. import java.awt.Color;
  28. import java.awt.Component;
  29. import java.awt.Dimension;
  30. import java.awt.Graphics;
  31. import java.awt.Polygon;
  32.  
  33. import java.io.Serializable;
  34.  
  35. /**
  36.  * Icon factory for the CDE/Motif Look and Feel
  37.  * <p>
  38.  * Warning: serialized objects of this class will not be compatible with
  39.  * future swing releases.  The current serialization support is appropriate
  40.  * for short term storage or RMI between Swing1.0 applications.  It will
  41.  * not be possible to load serialized Swing1.0 objects with future releases
  42.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  43.  * baseline for the serialized form of Swing objects.
  44.  *
  45.  * 1.10 02/02/98
  46.  * @author Georges Saab
  47.  */
  48. public class MotifIconFactory implements Serializable
  49. {
  50.     private static Icon checkBoxIcon;
  51.     private static Icon radioButtonIcon;
  52.     private static Icon menuItemCheckIcon;
  53.     private static Icon menuItemArrowIcon;
  54.     private static Icon menuArrowIcon;
  55.  
  56.     public static Icon getMenuItemCheckIcon() {
  57.     if (menuItemCheckIcon == null) {
  58.         menuItemCheckIcon = new MenuItemCheckIcon();
  59.     }
  60.     return menuItemCheckIcon;
  61.     }
  62.  
  63.     public static Icon getMenuItemArrowIcon() {
  64.     if (menuItemArrowIcon == null) {
  65.         menuItemArrowIcon = new MenuItemArrowIcon();
  66.     }
  67.     return menuItemArrowIcon;
  68.     }
  69.  
  70.     public static Icon getMenuArrowIcon() {
  71.     if (menuArrowIcon == null) {
  72.         menuArrowIcon = new MenuArrowIcon();
  73.     }
  74.     return menuArrowIcon;
  75.     }
  76.  
  77.     public static Icon getCheckBoxIcon() {
  78.     if (checkBoxIcon == null) {
  79.         checkBoxIcon = new CheckBoxIcon();
  80.     }
  81.     return checkBoxIcon;
  82.     }
  83.  
  84.     public static Icon getRadioButtonIcon() {
  85.     if (radioButtonIcon == null) {
  86.         radioButtonIcon = new RadioButtonIcon();
  87.     }
  88.     return radioButtonIcon;
  89.     }
  90.  
  91.     private static class CheckBoxIcon implements Icon, UIResource, Serializable  {
  92.     final static int csize = 13;
  93.  
  94.     public void paintIcon(Component c, Graphics g, int x, int y){
  95.         AbstractButton b = (AbstractButton) c;
  96.         ButtonModel model = b.getModel();
  97.  
  98.         boolean isPressed = model.isPressed();
  99.         boolean isArmed = model.isArmed();
  100.         boolean isEnabled = model.isEnabled();
  101.         boolean isSelected = model.isSelected();
  102.         
  103.         // 4 -looks- to the Motif CheckBox
  104.         // drawCheckBezelOut - default unchecked state
  105.         // drawBezel - when we uncheck in toggled state
  106.         // drawCheckBezel - when we check in toggle state
  107.         // drawCheckBezelIn - selected, mouseReleased
  108.         boolean checkToggleIn = ((isPressed && 
  109.                       !isArmed   &&
  110.                       isSelected) ||
  111.                      (isPressed &&
  112.                       isArmed   &&
  113.                       !isSelected));
  114.         boolean uncheckToggleOut = ((isPressed && 
  115.                      !isArmed &&
  116.                      !isSelected) ||
  117.                     (isPressed &&
  118.                      isArmed &&
  119.                      isSelected));
  120.         
  121.         boolean checkIn = (!isPressed  &&
  122.                    isArmed    &&
  123.                    isSelected  ||
  124.                    (!isPressed &&
  125.                 !isArmed  &&
  126.                 isSelected));
  127.         
  128.         x += 2;
  129.         if (checkToggleIn)
  130.         {
  131.             // toggled from unchecked to checked
  132.             drawCheckBezel(g,x,y,csize,true,false,false);
  133.         }
  134.         else if (uncheckToggleOut)
  135.         {
  136.             //        MotifBorderFactory.drawBezel(g,x,y,csize,csize,false,false);
  137.             drawCheckBezel(g,x,y,csize,true,true,false);
  138.             
  139.         }      
  140.         else if (checkIn)
  141.         { 
  142.             // show checked, unpressed state
  143.             drawCheckBezel(g,x,y,csize,false,false,true);
  144.         }
  145.         else 
  146.         { //  show unchecked state
  147.             drawCheckBezelOut(g,x,y,csize);
  148.         }
  149.     }
  150.     
  151.     public int getIconWidth() {
  152.         return csize;
  153.     }
  154.     
  155.     public int getIconHeight() {
  156.         return csize;
  157.     }
  158.     
  159.     public void drawCheckBezelOut(Graphics g, int x, int y, int csize){
  160.         Color controlShadow = UIManager.getColor("controlShadow");
  161.         Color controlHighlight = UIManager.getColor("controlHighlight");
  162.         
  163.         int w = csize;
  164.         int h = csize;
  165.         Color oldColor = g.getColor();    
  166.         
  167.         g.translate(x,y);
  168.         g.setColor(controlHighlight);    // inner 3D border
  169.         g.drawLine(0, 0, 0, h-1);
  170.         g.drawLine(1, 0, w-3, 0);
  171.         
  172.         g.setColor(controlShadow);         // black drop shadow  __|
  173.         g.drawLine(0, h-1, w-1, h-1);
  174.         g.drawLine(w-1, h-1, w-1, 0);
  175.         g.translate(-x,-y);
  176.         g.setColor(oldColor);
  177.     }
  178.     
  179.     public void drawCheckBezel(Graphics g, int x, int y, int csize, 
  180.                    boolean shade, boolean out, boolean check)
  181.         {
  182.         Color controlLightShadow = UIManager.getColor("controlLightShadow");
  183.         Color control = UIManager.getColor("control");
  184.         Color controlShadow = UIManager.getColor("controlShadow");
  185.         Color controlLight = UIManager.getColor("controlLtHighlight");
  186.         Color controlHighlight = UIManager.getColor("controlHighlight");
  187.         
  188.         Color oldColor = g.getColor();    
  189.         g.translate(x, y);
  190.         
  191.         if (out){
  192.             g.setColor(control);
  193.             g.fillRect(0,0,csize,csize);
  194.         }
  195.         else {
  196.             g.setColor(controlLightShadow);
  197.             g.fillRect(0,0,csize,csize);
  198.         }
  199.         
  200.         //bottom
  201.         if (out)
  202.             g.setColor(controlShadow);
  203.         else
  204.             g.setColor(controlLight);
  205.         g.drawLine(1,csize-1,csize-2,csize-1); 
  206.         if (shade) {
  207.             g.drawLine(2,csize-2,csize-3,csize-2);
  208.             g.drawLine(csize-2,2,csize-2 ,csize-1);
  209.             if (out)
  210.             g.setColor(controlLight);
  211.             else
  212.             g.setColor(controlShadow);
  213.             g.drawLine(1,2,1,csize-2);
  214.             g.drawLine(1,1,csize-3,1);
  215.             if (out)
  216.             g.setColor(controlShadow);
  217.             else
  218.             g.setColor(controlLight);
  219.         }
  220.         //right
  221.         g.drawLine(csize-1,1,csize-1,csize-1);
  222.         
  223.         //left
  224.         if (out)
  225.             g.setColor(controlLight);
  226.         else
  227.             g.setColor(controlShadow);
  228.         g.drawLine(0,1,0,csize-1);
  229.         
  230.         //top
  231.         g.drawLine(0,0,csize-1,0); 
  232.         
  233.         if (check){
  234.             // draw check 
  235.             g.setColor(Color.black);
  236.             g.drawLine(csize-2,1,csize-2,2);
  237.             g.drawLine(csize-3,2,csize-3,3);
  238.             g.drawLine(csize-4,3,csize-4,4);
  239.             g.drawLine(csize-5,4,csize-5,6);
  240.             g.drawLine(csize-6,5,csize-6,8);
  241.             g.drawLine(csize-7,6,csize-7,10);
  242.             g.drawLine(csize-8,7,csize-8,10);
  243.             g.drawLine(csize-9,6,csize-9,9);
  244.             g.drawLine(csize-10,5,csize-10,8);
  245.             g.drawLine(csize-11,5,csize-11,7);
  246.             g.drawLine(csize-12,6,csize-12,6);
  247.         }
  248.         g.translate(-x, -y);
  249.         g.setColor(oldColor);
  250.         }
  251.     } // end class CheckBoxIcon
  252.     
  253.     private static class RadioButtonIcon implements Icon, UIResource, Serializable {
  254.     public void paintIcon(Component c, Graphics g, int x, int y) {
  255.         Color dot = UIManager.getColor("activeCaptionBorder");
  256.         // fill interior
  257.         AbstractButton b = (AbstractButton) c;
  258.         ButtonModel model = b.getModel();
  259.         
  260.         int w = getIconWidth();
  261.         int h = getIconHeight();
  262.  
  263.         // add pad so focus isn't smudged on the x
  264.         x += 2;
  265.  
  266.         boolean isPressed = model.isPressed();
  267.         boolean isArmed = model.isArmed();
  268.         boolean isEnabled = model.isEnabled();
  269.         boolean isSelected = model.isSelected();
  270.         
  271.         boolean checkIn = ((isPressed && 
  272.                 !isArmed   &&
  273.                 isSelected) ||
  274.                    (isPressed &&
  275.                 isArmed   &&
  276.                 !isSelected) 
  277.                    ||
  278.                    (!isPressed  &&
  279.                 isArmed    &&
  280.                 isSelected  ||
  281.                 (!isPressed &&
  282.                  !isArmed  &&
  283.                  isSelected)));
  284.         
  285.         if (checkIn){
  286.                 g.setColor(Color.gray);
  287.         g.drawLine(x+5,y+0,x+8,y+0);
  288.         g.drawLine(x+3,y+1,x+4,y+1);
  289.         g.drawLine(x+9,y+1,x+9,y+1);
  290.         g.drawLine(x+2,y+2,x+2,y+2);
  291.         g.drawLine(x+1,y+3,x+1,y+3);
  292.         g.drawLine(x,y+4,x,y+9);
  293.         g.drawLine(x+1,y+10,x+1,y+10);
  294.         g.drawLine(x+2,y+11,x+2,y+11);
  295.         g.setColor(Color.white);
  296.         g.drawLine(x+3,y+12,x+4,y+12);
  297.         g.drawLine(x+5,y+13,x+8,y+13);
  298.         g.drawLine(x+9,y+12,x+10,y+12);
  299.         g.drawLine(x+11,y+11,x+11,y+11);
  300.         g.drawLine(x+12,y+10,x+12,y+10);
  301.         g.drawLine(x+13,y+9,x+13,y+4);
  302.         g.drawLine(x+12,y+3,x+12,y+3);
  303.         g.drawLine(x+11,y+2,x+11,y+2);
  304.         g.drawLine(x+10,y+1,x+10,y+1);
  305.         g.setColor(dot);
  306.         g.fillRect(x+4,y+5,6,4);
  307.         g.drawLine(x+5,y+4,x+8,y+4);
  308.         g.drawLine(x+5,y+9,x+8,y+9);
  309.         }
  310.         else {
  311.         g.setColor(Color.white);
  312.         g.drawLine(x+5,y+0,x+8,y+0);
  313.         g.drawLine(x+3,y+1,x+4,y+1);
  314.         g.drawLine(x+9,y+1,x+9,y+1);
  315.         g.drawLine(x+2,y+2,x+2,y+2);
  316.         g.drawLine(x+1,y+3,x+1,y+3);
  317.         g.drawLine(x,y+4,x,y+9);
  318.         g.drawLine(x+1,y+10,x+1,y+10);
  319.         g.drawLine(x+2,y+11,x+2,y+11);
  320.  
  321.         g.setColor(Color.gray);
  322.         g.drawLine(x+3,y+12,x+4,y+12);
  323.         g.drawLine(x+5,y+13,x+8,y+13);
  324.         g.drawLine(x+9,y+12,x+10,y+12);
  325.         g.drawLine(x+11,y+11,x+11,y+11);
  326.         g.drawLine(x+12,y+10,x+12,y+10);
  327.         g.drawLine(x+13,y+9,x+13,y+4);
  328.         g.drawLine(x+12,y+3,x+12,y+3);
  329.         g.drawLine(x+11,y+2,x+11,y+2);
  330.         g.drawLine(x+10,y+1,x+10,y+1);
  331.  
  332.         }
  333.     }
  334.     
  335.     public int getIconWidth() {
  336.         return 13;
  337.     }
  338.     
  339.     public int getIconHeight() {
  340.         return 13;
  341.     }
  342.     } // end class RadioButtonIcon
  343.  
  344.     private static class MenuItemCheckIcon implements Icon, UIResource, Serializable
  345.     {
  346.     public void paintIcon(Component c,Graphics g, int x, int y) 
  347.         {
  348.         }
  349.     public int getIconWidth() { return 0; }
  350.     public int getIconHeight() { return 0; }
  351.     }  // end class MenuItemCheckIcon
  352.     
  353.     
  354.     private static class MenuItemArrowIcon implements Icon, UIResource, Serializable
  355.     {
  356.     public void paintIcon(Component c,Graphics g, int x, int y) 
  357.         {
  358.         }
  359.     public int getIconWidth() { return 0; }
  360.     public int getIconHeight() { return 0; }
  361.     }  // end class MenuItemArrowIcon
  362.     
  363.     private static class MenuArrowIcon implements Icon, UIResource, Serializable 
  364. {
  365.    private Color controlFocus = UIManager.getColor("windowBorder");
  366.    private Color controlShadow = UIManager.getColor("controlShadow");
  367.    private Color lightShadow = UIManager.getColor("controlLtHighlight");
  368.    private Color controlHighlight = UIManager.getColor("controlHighlight");
  369.    private Color ltHighlight = UIManager.getColor("controlLtHightlight");
  370.  
  371.  
  372.    public void paintIcon(Component c, Graphics g, int x, int y) {
  373.        AbstractButton b = (AbstractButton) c;
  374.        ButtonModel model = b.getModel();
  375.  
  376.        int w = getIconWidth();
  377.        int h = getIconHeight();
  378.  
  379.  
  380.        Color oldColor = g.getColor();
  381.        // REMIND: this is a mess
  382.  
  383.        if (model.isSelected()){
  384.      Polygon p = new Polygon();
  385.      p.addPoint(x+1,y);
  386.      p.addPoint(x+w-1,y+5);
  387.      p.addPoint(x+1,y+h+1);
  388.      g.setColor(controlFocus);
  389.      g.fillPolygon(p);
  390.      g.drawLine(x+5,y+h-2,x+5,y+h-2);
  391.      g.drawLine(x+7,y+h-3,x+7,y+h-3);
  392.      g.setColor(controlShadow);
  393.      g.drawLine(x+1,y+1,x+1,y+h);
  394.      g.drawLine(x+2,y+1,x+2,y+1);
  395.      g.drawLine(x+4,y+2,x+4,y+2);
  396.      g.drawLine(x+6,y+3,x+6,y+3);
  397.      g.drawLine(x+8,y+4,x+8,y+5);
  398.      g.setColor(lightShadow);
  399.      g.drawLine(x+2,y+h,x+2,y+h);
  400.      g.drawLine(x+4,y+h-1,x+4,y+h-1);
  401.      g.drawLine(x+6,y+h-2,x+6,y+h-2);
  402.      g.drawLine(x+8,y+h-3,x+8,y+h-4);
  403.        }
  404.        else {
  405.      g.setColor(lightShadow);
  406.      g.drawLine(x+1,y+1,x+1,y+h);
  407.      g.drawLine(x+2,y+1,x+2,y+h-2);
  408.      g.fillRect(x+3,y+2,2,2);
  409.      g.fillRect(x+5,y+3,2,2);
  410.      g.fillRect(x+7,y+4,2,2);
  411.      g.setColor(controlShadow);
  412.      g.drawLine(x+2,y+h-1,x+2,y+h);
  413.      g.fillRect(x+3,y+h-2,2,2);
  414.      g.fillRect(x+5,y+h-3,2,2);
  415.      g.fillRect(x+7,y+h-4,2,2);
  416.      g.setColor(oldColor);
  417.        }
  418.  
  419.      }
  420.      public int getIconWidth() { return 10; }
  421.      public int getIconHeight() { return 10; }
  422.     } // End class MenuArrowIcon
  423. }
  424.